<?php

namespace app\store\controller\goods;

use app\store\controller\Controller;
use app\store\model\Search as SearchModel;

/**
 * 商品搜索
 */
class Search extends Controller{
    /**
     * 列表
     */
    public function index(){
        $model = new SearchModel;
        $list = $model->getAll();
        return $this->fetch('index', compact('list'));
    }

    /**
     * 删除
     */
    public function delete($id){
        $model = new SearchModel;
        if (!$model->remove($id)) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 添加
     */
    public function add(){
        $model = new SearchModel;
        if (!$this->request->isAjax()) {
            return $this->fetch('add');
        }
        if ($model->add($this->postData('search'))) {
            return $this->renderSuccess('添加成功', url('goods.search/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑
     */
    public function edit($id){
        $model = SearchModel::get($id);
        if (!$this->request->isAjax()) {
            $list = $model;
            return $this->fetch('edit', compact('model', 'list'));
        }
        // 更新记录
        if ($model->edit($this->postData('search'))) {
            return $this->renderSuccess('更新成功', url('goods.search/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

}
